package object;

import utils.FileCreation;

import java.io.File;
import java.io.IOException;

public class Repository {
    private static String workTree;	//working directory
    private static String gitDir;	//jit Repository path

    /**
     * Constructor
     */
    public Repository() throws IOException {
        if(gitDir == ""){
            throw new IOException("The Repository does not exist!");
        }
    }

    /**
     * Construct a new Repository instance with certain path.
     * Constructor
     * @param path
     * @throws IOException
     */
    public Repository(String path) throws IOException {
        this.workTree = path;
        this.gitDir = path + File.separator + ".jit";
    }

    public static String getGitDir() {
        return gitDir;
    }

    public static String getWorkTree() {
        return workTree;
    }

    /**
     * Helper functions.
     * @return
     */
    public boolean exist(){ return new File(gitDir).exists(); }

    public boolean isFile(){ return new File(gitDir).isFile(); }

    public boolean isDirectory(){ return new File(gitDir).isDirectory(); }


    /**
     * Create the Repository and files and directories inside.
     * @return boolean
     * @throws IOException
     */
    public void createRepo() throws IOException, Exception {
        //创建缓存区类

        FileCreation.createDirectory(workTree, ".jit");//创建仓库目录
        FileCreation.createDirectory(gitDir, "objects");//创建objects目录
        FileCreation.createDirectory(gitDir, "refs");//创建refs目录
        FileCreation.createDirectory(gitDir, "refs", "heads");
        FileCreation.createDirectory(gitDir, "logs");//创建logs目录
    }

}
